package com.my.main.controller.sysConfig;


/**
 * 系统配置--配置项管理
 *
 * @author liuhx 2020-04-28
 */


import com.my.main.base.controller.BaseController;
import com.my.main.base.entity.Result;
import com.my.main.base.entity.ResultCode;
import com.my.main.common.utils.CurrencyUtil;
import com.my.main.entity.sysConfig.SysConfig;
import com.my.main.service.sysConfig.SysConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//1.解决跨域
@CrossOrigin
//2.声明restContoller
@RestController
//3.设置父路径
@RequestMapping(value="/api/v1/sys/SysConfig")
public class SysConfigController extends BaseController {


    @Autowired
    private SysConfigService sysConfigService;


    /**
     * 查询 - 系统配置--配置项列表查询
     *
     * @param map(page: 页数, limit: 每页条数)
     *
     * @author liuhx 2020-04-28
     */
    @ApiOperation(value="系统配置 - 配置项列表查询")
    @RequestMapping(value = "/querySysConfigList", method = RequestMethod.POST)
    public Result querySysConfigList(@RequestBody Map<String, Object> map) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("page", map.get("page"));
        resMap.put("limit", map.get("limit"));

        try {
            // 查询总条数
            Long count = sysConfigService.queryCount();
            resMap.put("totalCount", count);

            // 查询指定条数，页数的会议室信息
            List<Map<String, Object>> sysConfigList = new ArrayList<>();

            map.put("page", CurrencyUtil.PageRowNum(Integer.parseInt(map.get("page").toString()), Integer.parseInt(map.get("limit").toString())));
            List<SysConfig> sysConfigs = sysConfigService.queryList(map);
            for(SysConfig sysConfig: sysConfigs) {
                Map<String, Object> sysConfigMap = new HashMap<>();
                // 配置项 Key
                sysConfigMap.put("configKey", sysConfig.getConfigKey());
                // 配置项 value
                sysConfigMap.put("value", sysConfig.getValue());
                // 配置项 备注
                sysConfigMap.put("comment", sysConfig.getComment());
                // 整理数据
                sysConfigList.add(sysConfigMap);
            }
            resMap.put("sysConfigList", sysConfigList);
            return new Result(ResultCode.SUCCESS, resMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAIL, "系统错误，请联系管理员");
        }
    }


    /**
     * 修改 - 系统配置--配置项修改
     *
     * @param sysConfig
     *
     * @author liuhx 2020-02-24
     */
    @ApiOperation(value="系统配置 - 配置项修改")
    @RequestMapping(value = "/updateSysConfigByKey", method = RequestMethod.POST)
    public Result updateSysConfigByKey(@RequestBody SysConfig sysConfig) {
        if (StringUtils.isEmpty(sysConfig.getConfigKey())) {
            return new Result(ResultCode.FAIL);
        }

        try {
            // 修改系统配置--配置项
            sysConfigService.isUpdate(sysConfig);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAIL,"系统错误，请联系管理员");
        }

    }

}
